package com.iamteer.day22;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

/**
主要回顾：PrintStream

打印流可以打印任意类型的数据（打印前会转换）

 */
class User1{
	int id;
	String name;
	public User1(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "{" + this.id + "\t" + this.name + "}";
	}
}

public class PrintStream_eg {

	public static void main(String[] args) throws IOException {
		testPrintStream();
		

	}
	
	public static void testPrintStream() throws IOException {
		//覆盖模式
		PrintStream pStream = new PrintStream(new File("print.txt"));
		//追加模式
//		PrintStream pStream = new PrintStream(new FileOutputStream(new File("print.txt"), true));
		pStream.println("test print stream");	//打印字符串
		pStream.println(-1);					//打印整数
		pStream.println(3.14159);				//打印浮点数
		pStream.println(true);					//打印布尔值
		pStream.println(new User1(001, "Chuck"));//打印对象（要借助 toString 方法）
		
		//默认标准的输出流就是向控制台输出
		System.setOut(pStream);
		System.out.println("where am I?");	//把输出重定向到文件
		
		//收集异常的日志信息
		try {
			int c = 4/0;
			System.out.println("c = " + c);
		} catch (Exception e) {
			e.printStackTrace(pStream);	//把日志输出重定向到文件
		}
	}

}
